Load all required libraries.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.6 v dplyr 1.0.8
## v tidyr 1.2.0 v stringr 1.4.0
## v readr 2.1.2 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
Read in raw data from RDS.
raw_data <- readRDS("./year2.RDS")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2021-06-30"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2021-06-30"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.25, n = 421)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 11.41148 11.46350 11.51461 11.56484 11.61421 11.66274 11.71048 11.75743
## [9] 11.80363 11.84911 11.89388 11.93798 11.98144 12.02427 12.06651 12.10811
## [17] 12.14901 12.18919 12.22862 12.26728 12.30514 12.34218 12.37838 12.41372
## [25] 12.44816 12.48169 12.51429 12.54592 12.57657 12.60659 12.63627 12.66552
## [33] 12.69424 12.72234 12.74972 12.77628 12.80193 12.82658 12.85012 12.87245
## [41] 12.89350 12.91315 12.93131 12.94912 12.96756 12.98628 13.00495 13.02323
## [49] 13.04078 13.05726 13.07232 13.08564 13.09687 13.10568 13.11171 13.11506
## [57] 13.11615 13.11513 13.11219 13.10748 13.10118 13.09346 13.08447 13.07440
## [65] 13.06340 13.05164 13.03930 13.02654 13.01353 13.00043 12.98321 12.95908
## [73] 12.93012 12.89837 12.86591 12.83480 12.80710 12.78487 12.76399 12.73937
## [81] 12.71163 12.68141 12.64933 12.61603 12.58212 12.54826 12.51505 12.48314
## [89] 12.45314 12.42570 12.40144 12.38098 12.36237 12.34337 12.32418 12.30503
## [97] 12.28613 12.26768 12.24990 12.23302 12.21723 12.20275 12.18981 12.17860
## [105] 12.16935 12.16199 12.15633 12.15216 12.14929 12.14752 12.14666 12.14650
## [113] 12.14684 12.14749 12.14825 12.14893 12.14932 12.14922 12.14989 12.15250
## [121] 12.15677 12.16240 12.16909 12.17655 12.18447 12.19258 12.20056 12.20812
## [129] 12.21497 12.22082 12.22536 12.22830 12.22953 12.22933 12.22801 12.22585
## [137] 12.22317 12.22025 12.21741 12.21493 12.21313 12.21229 12.21272 12.21471
## [145] 12.21858 12.22460 12.23201 12.23982 12.24804 12.25670 12.26581 12.27539
## [153] 12.28545 12.29601 12.30708 12.31869 12.33084 12.34356 12.35687 12.37077
## [161] 12.38719 12.40761 12.43133 12.45767 12.48597 12.51552 12.54566 12.57571
## [169] 12.60498 12.63279 12.65846 12.68132 12.70067 12.71585 12.73118 12.75097
## [177] 12.77446 12.80089 12.82953 12.85961 12.89039 12.92111 12.95102 12.97937
## [185] 13.00541 13.02838 13.04754 13.06214 13.07142 13.07463 13.07342 13.07004
## [193] 13.06461 13.05725 13.04809 13.03726 13.02488 13.01108 12.99598 12.97972
## [201] 12.96241 12.94418 12.92515 12.90546 12.88099 12.84846 12.80925 12.76476
## [209] 12.71638 12.66549 12.61349 12.56177 12.51171 12.46471 12.42215 12.38542
## [217] 12.34963 12.30948 12.26574 12.21919 12.17061 12.12077 12.07044 12.02040
## [225] 11.97143 11.92430 11.87980 11.83868 11.80174 11.76975 11.73988 11.70903
## [233] 11.67758 11.64588 11.61429 11.58317 11.55290 11.52382 11.49631 11.47072
## [241] 11.44743 11.42678 11.40914 11.39488 11.38343 11.37393 11.36629 11.36042
## [249] 11.35624 11.35365 11.35258 11.35292 11.35460 11.35752 11.36160 11.36675
## [257] 11.37287 11.37990 11.39167 11.41082 11.43544 11.46361 11.49342 11.52294
## [265] 11.55026 11.57346 11.59063 11.60591 11.62435 11.64548 11.66878 11.69376
## [273] 11.71991 11.74674 11.77374 11.80042 11.82628 11.85080 11.87351 11.89388
## [281] 11.91143 11.92720 11.94260 11.95771 11.97257 11.98727 12.00184 12.01637
## [289] 12.03090 12.04551 12.06026 12.07520 12.09040 12.10512 12.11870 12.13129
## [297] 12.14307 12.15420 12.16484 12.17516 12.18532 12.19550 12.20585 12.21655
## [305] 12.22775 12.23963 12.25234 12.26579 12.27968 12.29395 12.30852 12.32330
## [313] 12.33823 12.35322 12.36820 12.38308 12.39780 12.41228 12.42643 12.44018
## [321] 12.45345 12.46731 12.48270 12.49935 12.51700 12.53539 12.55424 12.57330
## [329] 12.59230 12.61098 12.62906 12.64630 12.66241 12.67715 12.69023 12.70141
## [337] 12.71045 12.71749 12.72283 12.72676 12.72955 12.73150 12.73290 12.73404
## [345] 12.73520 12.73667 12.73874 12.74171 12.74585 12.75072 12.75567 12.76064
## [353] 12.76557 12.77042 12.77514 12.77967 12.78397 12.78797 12.79164 12.79492
## [361] 12.79776 12.80011 12.80191 12.80378 12.80618 12.80890 12.81169 12.81432
## [369] 12.81655 12.81813 12.81885 12.81846 12.81622 12.81179 12.80556 12.79791
## [377] 12.78923 12.77988 12.77026 12.76074 12.75171 12.74354 12.73663 12.73134
## [385] 12.72807 12.72719 12.72742 12.72732 12.72700 12.72663 12.72633 12.72623
## [393] 12.72648 12.72721 12.72856 12.73066 12.73366 12.73768 12.74242 12.74746
## [401] 12.75282 12.75853 12.76460 12.77105 12.77789 12.78516 12.79286 12.80101
## [409] 12.80964 12.81875 12.82838 12.83853 12.84938 12.86107 12.87352 12.88666
## [417] 12.90041 12.91471 12.92948 12.94464 12.96014
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./site_objects/wrf_a_year2.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.25, n = 421)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 10.82076 10.90154 10.98093 11.05891 11.13544 11.21051 11.28409 11.35615
## [9] 11.42667 11.49563 11.56298 11.62872 11.69281 11.75523 11.81596 11.87502
## [17] 11.93248 11.98837 12.04270 12.09551 12.14682 12.19664 12.24502 12.29196
## [25] 12.33750 12.38165 12.42445 12.46591 12.50607 12.54458 12.58116 12.61590
## [33] 12.64888 12.68021 12.70996 12.73824 12.76513 12.79072 12.81511 12.83839
## [41] 12.86064 12.88195 12.90243 12.92071 12.93565 12.94768 12.95721 12.96468
## [49] 12.97051 12.97511 12.97891 12.98233 12.98580 12.98974 12.99457 12.99915
## [57] 13.00216 13.00373 13.00397 13.00301 13.00096 12.99796 12.99413 12.98959
## [65] 12.98445 12.97886 12.97292 12.96677 12.96052 12.95429 12.94753 12.93970
## [73] 12.93096 12.92148 12.91143 12.90097 12.89028 12.87951 12.86858 12.85727
## [81] 12.84555 12.83338 12.82073 12.80757 12.79386 12.77957 12.76466 12.74911
## [89] 12.73288 12.71594 12.69824 12.67977 12.65892 12.63456 12.60735 12.57795
## [97] 12.54701 12.51518 12.48313 12.45152 12.42099 12.39221 12.36584 12.34253
## [105] 12.31838 12.28968 12.25731 12.22217 12.18516 12.14719 12.10914 12.07191
## [113] 12.03641 12.00352 11.97416 11.94921 11.92957 11.91615 11.90720 11.90026
## [121] 11.89518 11.89181 11.88999 11.88956 11.89037 11.89225 11.89505 11.89862
## [129] 11.90280 11.90744 11.91237 11.91743 11.92404 11.93348 11.94544 11.95960
## [137] 11.97565 11.99327 12.01214 12.03196 12.05239 12.07314 12.09388 12.11430
## [145] 12.13408 12.15290 12.17381 12.19954 12.22934 12.26247 12.29819 12.33574
## [153] 12.37437 12.41334 12.45190 12.48930 12.52479 12.55763 12.58707 12.61235
## [161] 12.63638 12.66226 12.68961 12.71803 12.74714 12.77655 12.80586 12.83469
## [169] 12.86264 12.88932 12.91435 12.93732 12.95785 12.97556 12.99277 13.01181
## [177] 13.03232 13.05389 13.07614 13.09869 13.12114 13.14311 13.16422 13.18407
## [185] 13.20227 13.21845 13.23221 13.24317 13.25094 13.25513 13.25732 13.25920
## [193] 13.26060 13.26133 13.26120 13.26002 13.25761 13.25377 13.24833 13.24109
## [201] 13.23187 13.22048 13.20673 13.19044 13.16936 13.14204 13.10952 13.07280
## [209] 13.03292 12.99090 12.94776 12.90453 12.86223 12.82189 12.78452 12.75116
## [217] 12.71786 12.68044 12.63954 12.59582 12.54991 12.50248 12.45415 12.40559
## [225] 12.35744 12.31035 12.26496 12.22192 12.18189 12.14550 12.10938 12.07014
## [233] 12.02843 11.98487 11.94010 11.89476 11.84947 11.80488 11.76162 11.72032
## [241] 11.68161 11.64614 11.61454 11.58744 11.56298 11.53903 11.51573 11.49326
## [249] 11.47180 11.45151 11.43255 11.41511 11.39935 11.38544 11.37355 11.36385
## [257] 11.35651 11.35170 11.35104 11.35532 11.36347 11.37444 11.38715 11.40054
## [265] 11.41354 11.42508 11.43410 11.44309 11.45502 11.46953 11.48624 11.50479
## [273] 11.52481 11.54594 11.56781 11.59006 11.61231 11.63421 11.65539 11.67547
## [281] 11.69410 11.71357 11.73607 11.76106 11.78802 11.81642 11.84572 11.87541
## [289] 11.90495 11.93381 11.96147 11.98739 12.01104 12.03409 12.05838 12.08368
## [297] 12.10973 12.13630 12.16314 12.19002 12.21668 12.24289 12.26840 12.29297
## [305] 12.31636 12.33833 12.35863 12.37786 12.39677 12.41536 12.43363 12.45159
## [313] 12.46922 12.48654 12.50353 12.52021 12.53656 12.55259 12.56829 12.58367
## [321] 12.59873 12.61359 12.62836 12.64301 12.65750 12.67182 12.68593 12.69980
## [329] 12.71340 12.72670 12.73967 12.75228 12.76451 12.77632 12.78768 12.79856
## [337] 12.80822 12.81611 12.82252 12.82770 12.83195 12.83552 12.83870 12.84175
## [345] 12.84495 12.84858 12.85291 12.85821 12.86476 12.87222 12.88004 12.88814
## [353] 12.89646 12.90492 12.91344 12.92196 12.93040 12.93869 12.94676 12.95453
## [361] 12.96194 12.96891 12.97536 12.98167 12.98815 12.99473 13.00129 13.00773
## [369] 13.01396 13.01988 13.02538 13.03037 13.03459 13.03793 13.04055 13.04258
## [377] 13.04418 13.04549 13.04665 13.04781 13.04912 13.05073 13.05277 13.05539
## [385] 13.05874 13.06296 13.06758 13.07204 13.07640 13.08069 13.08497 13.08928
## [393] 13.09366 13.09816 13.10283 13.10771 13.11285 13.11828 13.12391 13.12960
## [401] 13.13534 13.14116 13.14706 13.15304 13.15911 13.16529 13.17158 13.17798
## [409] 13.18451 13.19118 13.19798 13.20494 13.21212 13.21956 13.22724 13.23513
## [417] 13.24320 13.25143 13.25978 13.26822 13.27674
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./site_objects/wrf_b_year2.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.25, n = 421)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 10.86193 10.92146 10.97999 11.03752 11.09403 11.14953 11.20402 11.25748
## [9] 11.30991 11.36132 11.41168 11.46101 11.50930 11.55653 11.60272 11.64788
## [17] 11.69206 11.73525 11.77744 11.81864 11.85882 11.89799 11.93614 11.97327
## [25] 12.00938 12.04444 12.07847 12.11145 12.14338 12.17414 12.20364 12.23192
## [33] 12.25902 12.28499 12.30987 12.33370 12.35652 12.37838 12.39931 12.41936
## [41] 12.43858 12.45700 12.47467 12.49156 12.50761 12.52282 12.53717 12.55066
## [49] 12.56327 12.57500 12.58585 12.59579 12.60482 12.61293 12.62012 12.62632
## [57] 12.63150 12.63568 12.63888 12.64113 12.64243 12.64281 12.64230 12.64091
## [65] 12.63867 12.63559 12.63169 12.62700 12.62154 12.61533 12.60653 12.59398
## [73] 12.57871 12.56172 12.54404 12.52668 12.51067 12.49703 12.48304 12.46567
## [81] 12.44546 12.42297 12.39874 12.37330 12.34720 12.32099 12.29521 12.27040
## [89] 12.24710 12.22587 12.20724 12.19175 12.17808 12.16455 12.15118 12.13801
## [97] 12.12506 12.11235 12.09992 12.08779 12.07598 12.06453 12.05346 12.04279
## [105] 12.03262 12.02299 12.01387 12.00524 11.99704 11.98926 11.98186 11.97481
## [113] 11.96807 11.96162 11.95541 11.94942 11.94361 11.93796 11.93272 11.92812
## [121] 11.92408 11.92053 11.91739 11.91457 11.91200 11.90961 11.90730 11.90501
## [129] 11.90265 11.90015 11.89743 11.89441 11.89007 11.88374 11.87583 11.86677
## [137] 11.85698 11.84688 11.83688 11.82741 11.81888 11.81173 11.80636 11.80320
## [145] 11.80267 11.80518 11.80922 11.81309 11.81694 11.82092 11.82519 11.82989
## [153] 11.83520 11.84124 11.84819 11.85620 11.86541 11.87598 11.88807 11.90183
## [161] 11.91900 11.94075 11.96639 11.99521 12.02652 12.05962 12.09380 12.12837
## [169] 12.16263 12.19588 12.22742 12.25655 12.28257 12.30477 12.32799 12.35691
## [177] 12.39063 12.42826 12.46889 12.51162 12.55556 12.59981 12.64348 12.68565
## [185] 12.72543 12.76193 12.79424 12.82148 12.84273 12.85710 12.86754 12.87751
## [193] 12.88682 12.89530 12.90277 12.90906 12.91400 12.91741 12.91912 12.91895
## [201] 12.91674 12.91229 12.90545 12.89603 12.88076 12.85740 12.82732 12.79190
## [209] 12.75253 12.71057 12.66741 12.62441 12.58297 12.54445 12.51024 12.48171
## [217] 12.45328 12.41911 12.38018 12.33745 12.29191 12.24453 12.19629 12.14816
## [225] 12.10111 12.05613 12.01418 11.97624 11.94329 11.91630 11.89270 11.86932
## [233] 11.84626 11.82363 11.80153 11.78005 11.75930 11.73938 11.72038 11.70242
## [241] 11.68559 11.66999 11.65572 11.64288 11.63243 11.62499 11.62020 11.61769
## [249] 11.61709 11.61803 11.62014 11.62304 11.62638 11.62978 11.63287 11.63529
## [257] 11.63665 11.63660 11.63576 11.63509 11.63473 11.63484 11.63556 11.63704
## [265] 11.63942 11.64286 11.64749 11.65338 11.66037 11.66835 11.67719 11.68675
## [273] 11.69691 11.70753 11.71849 11.72966 11.74091 11.75211 11.76313 11.77385
## [281] 11.78412 11.79459 11.80588 11.81785 11.83039 11.84335 11.85660 11.87001
## [289] 11.88345 11.89678 11.90988 11.92262 11.93485 11.94784 11.96276 11.97929
## [297] 11.99714 12.01602 12.03561 12.05563 12.07577 12.09574 12.11523 12.13394
## [305] 12.15158 12.16785 12.18245 12.19631 12.21049 12.22485 12.23929 12.25368
## [313] 12.26790 12.28182 12.29534 12.30833 12.32066 12.33222 12.34288 12.35253
## [321] 12.36104 12.36850 12.37512 12.38098 12.38617 12.39076 12.39483 12.39847
## [329] 12.40176 12.40477 12.40760 12.41031 12.41300 12.41573 12.41860 12.42169
## [337] 12.42483 12.42782 12.43066 12.43333 12.43583 12.43816 12.44031 12.44228
## [345] 12.44405 12.44564 12.44702 12.44821 12.44918 12.44903 12.44704 12.44349
## [353] 12.43868 12.43288 12.42638 12.41948 12.41245 12.40558 12.39915 12.39346
## [361] 12.38879 12.38543 12.38365 12.38250 12.38093 12.37911 12.37721 12.37540
## [369] 12.37385 12.37274 12.37222 12.37248 12.37312 12.37366 12.37420 12.37480
## [377] 12.37553 12.37647 12.37769 12.37928 12.38129 12.38381 12.38691 12.39066
## [385] 12.39514 12.40042 12.40628 12.41246 12.41897 12.42583 12.43306 12.44066
## [393] 12.44866 12.45706 12.46589 12.47516 12.48488 12.49507 12.50569 12.51670
## [401] 12.52809 12.53987 12.55204 12.56461 12.57757 12.59094 12.60470 12.61887
## [409] 12.63344 12.64843 12.66382 12.67963 12.69588 12.71259 12.72975 12.74734
## [417] 12.76537 12.78382 12.80267 12.82193 12.84157
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./site_objects/wrf_c_year2.rda")
keeping in case
#save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
#save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
#save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
#save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
#save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
#save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
#save(both_ymina, file = "./plotly_objs/both_ymina.rda")
#save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
#save(both_yminb, file = "./plotly_objs/both_yminb.rda")
#save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
#save(both_yminc, file = "./plotly_objs/both_yminc.rda")
#save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")